﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;

using Visifire.Charts;
using sil;
using ESRI.ArcGIS.Client.Symbols;
using System.Threading;
using System.Windows.Threading;

using System.Collections.ObjectModel;

using GisWeb.ServiceReference1;
using ESRI.ArcGIS.Client.Toolkit;



namespace GisWeb
{
    public partial class MainPage : UserControl
    {
        Draw removeDraw;
        AutoResetEvent handle = new AutoResetEvent(true);
        //重心转移
        private void btnAbout3_Click(object sender, RoutedEventArgs e)
        {

            switch (mapclick)
            {
                case 0: 
                   removePara.Show();
                   removeDraw = new Draw(myMap);
                   MapTip tips = new MapTip();
                   tips.Template = LayoutRoot.Resources["maptip"] as ControlTemplate;
                   tips.GraphicsLayer = gra;
                    
                    mapclick = 5;
                    break;
                case 1: MessageBox.Show("正在运行基础查询模块，请先停止再进行查询"); break;
                case 2: MessageBox.Show("正在运行对比分析模块，请先停止再进行查询"); break;
                case 3: MessageBox.Show("正在运行经济指数预测模块，请先停止再进行查询"); break;
                case 4: MessageBox.Show("正在运行投资分析模块，请先停止再进行查询"); break;
                case 5: MessageBox.Show("正在运行重心转移模块，请先停止再进行查询"); break;

            }     
            
           

        }

        void qtask_ExecuteCompleted(object sender, QueryEventArgs e)
        {
            FeatureSet set = e.FeatureSet;
            if (set.Count() < 1)
            {
                MessageBox.Show("没有数据");




            }
            else
            {

                foreach (Graphic gra in set)
                {
                    Country cou = new Country();
                    cou.CountryName = gra.Attributes["NAME"].ToString();
                    ServiceReference1.Point point = new ServiceReference1.Point();


                    point.X = ((gra.Geometry) as MapPoint).X.ToString();
                    point.Y = ((gra.Geometry) as MapPoint).Y.ToString();
                    // MessageBox.Show(point.X);
                    cou.Point = point;
                    country.Add(cou);

                }

               

            }
        }




        void client_GetCenterCompleted(object sender, GetCenterCompletedEventArgs e)
        {

           
            Center cen = e.Result;          
                new Thread (() =>
                {
                    List<Graphic> graphs = new List<Graphic>();
                   // List<Graphic> center = new List<Graphic>();
                    int i = 1999;
                    foreach (ServiceReference1.Point p in cen.PointList){
                       
                        this.Dispatcher.BeginInvoke(() =>
                        {


                            MapPoint pt = new MapPoint();
                            pt.X = double.Parse(p.X);
                            pt.Y = double.Parse(p.Y);
                            Graphic graph = new Graphic();
                            graph.Symbol = LayoutRoot.Resources["Marker"] as MarkerSymbol;
                            graph.Geometry = pt;
                            graph.Attributes.Add("NAME",i+"年"+e.UserState.ToString()+"重心");

                            i++;
                            //Graphic c_point = new Graphic();
                            //c_point.Geometry = pt;
                            //c_point.Symbol = LayoutRoot.Resources["center"] as SimpleMarkerSymbol;
                            gra.Graphics.Add(graph);
                            //gra.Graphics.Add(c_point);
                         //   center.Add(c_point);
                            graphs.Add(graph);


                            //DispatcherTimer timer = new DispatcherTimer();
                            //timer.Tick += (s, t) => {   };
                            //timer.Interval = TimeSpan.FromMilliseconds(3000);
                            //timer.Start();



                        }

                        );
                        handle.WaitOne(3000);
                        //this.Dispatcher.BeginInvoke(() =>
                        //   {
                        //       gra.Graphics.Remove(center[i - 2000]);
                           
                           
                        //   });
                       
                    };
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        foreach (Graphic graph in graphs)
                        {
                            graph.Symbol = LayoutRoot.Resources["mark"] as SimpleMarkerSymbol;
                        }


                    });
                }).Start();



               


            
        }

        void removePara_Closed(object sender, EventArgs e)
        {
            mapclick = 0;
            gra.Graphics.Clear();
            removeDraw.IsEnabled = false;
            removePara.province.IsChecked = false;
            removePara.city.IsChecked = false;
            removePara.Close();

        }
        void removePara_clear_tip(object sender, EventArgs e)
        {
            gra.Graphics.Clear();
        }

        void removePara_stop_choose(object sender, EventArgs e)
        {
            removePara.city.IsChecked = false;
            removePara.province.IsChecked = false;
            removeDraw.IsEnabled = false;
        }

        void removePara_query_city(object sender, EventArgs e)
        {
            removeDraw.DrawMode = DrawMode.Point;
            removeDraw.IsEnabled = true;
            removeDraw.DrawComplete+=new EventHandler<DrawEventArgs>(remove_draw);
        }
        void remove_draw(object sender,DrawEventArgs e) {
            QueryTask queryTask = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/3");


            //查询后执行的操作
            queryTask.ExecuteCompleted += remove_querytask;

            Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            // Specify fields to return from query
            query.OutFields.Add("*");
            query.Geometry = e.Geometry;
            query.Where = "1=1";
            // Return geometry with result features
            query.ReturnGeometry = true;
            query.OutSpatialReference = myMap.SpatialReference;

            queryTask.ExecuteAsync(query);
        
        
        }
        void remove_querytask(object sender,QueryEventArgs e){
            FeatureSet set = e.FeatureSet;
            if (set.Count() < 1)
            {
                MessageBox.Show("找不到指定地点");


            }
            else {
                foreach (Graphic graph in set) {
                    myMap.ZoomTo(graph.Geometry);
                    ServiceReference1.GISServiceClient client = new GISServiceClient();
                    client.GetCenterCompleted += new EventHandler<GetCenterCompletedEventArgs>(client_GetCenterCompleted);
                    int type = removePara.type.SelectedIndex;
                    String movePara = "GDP";
                    switch (type) {
                        case 0: movePara = "GDP"; break;
                        case 1: movePara = "人口"; break;
                    
                    
                    }
                    client.GetCenterAsync(graph.Attributes["NAME"].ToString(), movePara, country,movePara);
                
                
                }
            
            
            }
        
        
        
        
        }

        void removePara_query_pro(object sender, EventArgs e)
        {
            
            ServiceReference1.GISServiceClient client = new GISServiceClient();
            client.GetCenterCompleted += new EventHandler<GetCenterCompletedEventArgs>(client_GetCenterCompleted);

            client.GetCenterAsync("湖南省", "GDP", country);
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
}